/**
 * NoteDao.java
 * com.ebook.server.dao
 * Copyright (c) 2013, 公司版权所有.
*/

package com.ebook.server.dao;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author    潘吉友
 * @Date	 2013-7-25 	 
 */
@Repository
public class DeviceBookDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	private static Log logger = LogFactory.getLog(DeviceBookDao.class);
	
	public void updateAfterSyncTime(long after_sync_time,String device_id,int bookid){
		String sql = "update device_book set after_sync_time=? where device_id=? and bookid=?";
		logger.info("|updateAfterSyncTime|"+sql);
		try{
			jdbcTemplate.update(sql, after_sync_time,device_id,bookid);
		}catch(Exception e){
			logger.error("|updateAfterSyncTime|update device_book error!");
			throw new RuntimeException(e);
		}
	}
	
	public long findNoteSyncTime(String device_id,int bookid){
		String sql = "select note_sync_time from device_book where device_id=? and bookid=? ";
		logger.info("|findNoteSyncTime|"+sql);
		try{
			Map<String,Object> map = jdbcTemplate.queryForMap(sql,device_id,bookid);
			return (Long)(map.get("note_sync_time"));
		}catch(Exception e){
			return -1;
		}
	}
	
	public boolean isExist(String device_id,int bookid){
		String sql = "select count(*) from device_book where device_id=? and bookid=?";
		logger.info("|isExist|"+sql);
		return jdbcTemplate.queryForInt(sql, device_id,bookid) > 0 ? true : false;
	}
	
	public void delete(String device_id){
		String sql = "delete from device_book where device_id= ?";
		logger.info("|delete|"+sql);
		jdbcTemplate.update(sql, device_id);
	}
	
	public String findKeys(String device_id,int bookid){
		String sql = "select `keys` from device_book where device_id=? and bookid=? ";
		logger.info("|findKeys|"+sql);
		try{
			Map<String,Object> map = jdbcTemplate.queryForMap(sql, device_id,bookid);
			if(!map.isEmpty()){
				return (String) map.get("keys");
			}else{
				return "error";
			}
		}catch(Exception e){
			logger.error("selece from device_book error!");
			throw new RuntimeException(e);
		}	
	}
	
	public long findAfterSyncTime(String device_id,int bookid){
		String sql = "select after_sync_time from device_book where device_id=? and bookid=? ";
		logger.info("|findAfterSyncTime|"+sql);
		try{
			Map<String,Object> map = jdbcTemplate.queryForMap(sql,device_id,bookid);
			return (Long)(map.get("after_sync_time"));
		}catch(Exception e){
			return 0;
		}
	}
	
	public void add(String device_id,int bookid,String keys){
		String sql = "insert into device_book (device_id,bookid,`keys`) values (?,?,?)";
		logger.info("|add|"+sql);
		try{
			jdbcTemplate.update(sql, device_id,bookid,keys);	
		}catch(Exception e){
			logger.error("|add|insert into device_book error!");
			throw new RuntimeException(e);
		}
	}
	
	public void updateKeys(String device_id,int bookid,String keys){
		String sql = "update device_book set `keys`=? where device_id=? and bookid=? ";
		logger.info("|updateKeys|"+sql);
		try{
			jdbcTemplate.update(sql, keys,device_id,bookid);
		}catch(Exception e){
			logger.error("|updateKeys|update device_book error!");
			throw new RuntimeException(e);
		}
	}
	
	public void updateNoteSyncTime(long note_sync_time,String device_id,int bookid){
		String sql = "update device_book set note_sync_time=? where device_id=? and bookid=?";
		logger.info("|updateNoteSyncTime|"+sql);
		try{
			jdbcTemplate.update(sql, note_sync_time,device_id,bookid);
		}catch(Exception e){
			logger.error("|updateNoteSyncTime|update device_book error!");
			throw new RuntimeException(e);
		}
	}
	
	public void updateReadingTime(int bookid,String device_id,long readingTime){
		String sql = "update device_book set reading_time=? where device_id=? and bookid=?";
		logger.info("|updateReadingTime|"+sql);
		try{
			jdbcTemplate.update(sql, readingTime,device_id,bookid);
		}catch(Exception e){
			logger.error("|updateReadingTime|update device_book error!");
			throw new RuntimeException(e);
		}
	}

}

